<?php namespace Common\Model;

use Hdphp\Model\Model;
use Common\Model\User;
use Common\Model\OrderList;
use Common\Model\Order;
use Common\Model\GoodsList;
use Common\Model\Goods;

/**
 * Class Order
 * @package Common\Model
 */
class Order extends Model{
	//指定表名
	protected $table = 'order';

	//自动验证
	protected $validate = [
		//array(字段名,验证方法,错误信息,验证条件,验证时机)

		//使用新地址验证
		['consignee','required','收货人必填',1,3],
		['address','required','收货地址必填',1,3],

		//确认支付验证
		['peyPassword','required','请填写支付密码',1,3],
		['peyPassword','_pay','支付密码错误',1,3],

		//验证余额够不够
		['money','_jianMoney','余额不足，请充值',1,3],//减用户的钱
	];

	//自动完成
	protected $auto = [
		//array('字段名','处理方法','方法类型',处理条件,处理时间);
		['number','getNum','method',1,3],
		['time','time','function',3,3],
		['wsshop_user_uid','getUid','method',1,3],
	];

	

	/**
	 * 判断用户的钱够不够，如果够久支付，如果不够就提示余额不足，请充值
	 * @return bool
	 */
	 public function _jianMoney(){
	 	$userModel = new User();
	 	$payPrice  = $_SESSION['cart']['total'];//获取支付的价格
		$account = $_SESSION['account'];//获取用户的账户
		$userMoney = Q('post.money');//获取用户的金钱
		if($userMoney < $payPrice ) return false;//如果余额比要支付的钱少，返回false
		$balance = $userMoney - $payPrice;//剩下的余额
		$re = $userModel->where("account = '{$account}' ")->update(['money'=>$balance]);//更新用户余额
		return true;
	 }

	

	/**
	 * 确认支付验证
	 * @return bool
	 */
	 public function _pay(){
	 	$userModel = new User();
	 	$account = $_SESSION['account'];
	 	$payPassword = md5(Q('post.peyPassword'));//获取当前密码输入的值
	 	$pwd = $userModel->where("account = '$account' ")->pluck('password');//获取用户表中的密码
		if($payPassword != $pwd) return false;//如果比对不成功返回false
		return true;//比对成功返回true
	 }

	/**
	 * 提交订单时获取订单好
	 * @return char
	 */
	 public function getNum(){
		return Cart::getOrderId();//返回订单号
	 }

	/**
	 * 提交订单时获取用户的uid
	 * @return uid
	 */
	public function getUid(){
		$userModel = new User();
		$uid = $userModel->where("account = '{$_SESSION['account']}' ")->pluck('uid');
		return $uid;
	}

	/**
	 * 使用新地址
	 * @return bool
	 */
	public function store(){
		if(!$this->create()) return false;//如果验证不成功就返回false
		$_SESSION['consignee'] = Q('post.consignee');
		$_SESSION['address'] = Q('post.address');
		return true;//添加成功后返回true
	}

	/**
	 * 提交订单
	 * @return bool
	 */
	public function addOrder(){
		$orderListModel = new OrderList();
		$this->create();
		$oid = $this->add();//添加订单表
		$orderListModel->store($oid);//调用订单列表的store方法
		$_SESSION['cart']['oid'] = $oid;//把订单列表的oid加到$_session中
		return true;
	}

	/**
	 * 确认支付
	 * @return bool
	 */
	 public function pay(){
		$goodsListModel = new GoodsList();
		$goodsModel = new Goods();
	 	if(!$this->create()) return false;//如果验证不成功就返回false

	 	//把订单表的支付状态变为待发货
	 	$orderModel = new Order();
		$oid = $_SESSION['cart']['oid'];
		$orderModel->where("oid = {$oid}")->update(['status'=>'待发货']);

		//减少对应商品列表中的库存
		foreach ($_SESSION['cart']['goods'] as $k => $v) {
			$glid = $v['options']['glid'];
			$inventory = $goodsListModel->where("glid = {$glid} ")->pluck('inventory');
			$inventory = $inventory - $v['num'];
			$goodsListModel->where("glid = {$glid} ")->update(['inventory'=>$inventory]);

			//修改商品表的总库存
			$total = $goodsListModel->where("wsshop_goods_gid= {$v['id']} ")->sum('inventory');
			$goodsModel->where("gid ={$v['id']} ")->update(['stock'=>$total]);
		}

	 	//支付成功后清空购物车
	 	unset($_SESSION['cart']);
	 	return true;
	 }
}
 ?>